//
//  PageViewController.m
//  Teacher
//
//  Created by cong on 15/10/27.
//  Copyright © 2015年 liangdianxiong. All rights reserved.
//

#import "PageViewController.h"
#import "Masonry.h"
#import "Header.h"
#import "LoginViewController.h"
@interface PageViewController ()<UIScrollViewDelegate>
//@property (weak, nonatomic) IBOutlet UIScrollView *Scroll_page;
@property(nonatomic,strong)UIPageControl *pageControl;

@property(nonatomic,strong)NSArray *imageNames;
@end

@implementation PageViewController


    
-(NSArray *)imageNames{
    if (!_imageNames) {
        _imageNames=@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];
    }
    return _imageNames;
}
-(void)viewDidLoad{
    [super viewDidLoad];
    
    
   
    UIScrollView *_Scroll_page=[[UIScrollView alloc]init];
    _Scroll_page.frame=self.view.frame;
    _Scroll_page.contentSize=CGSizeMake(_Scroll_page.frame.size.width*self.imageNames.count, _Scroll_page.frame.size.height);
    
    _Scroll_page.delegate = self;
    
    //向滚动视图加图片子视图
    NSInteger i;
    for ( i=0;i<self.imageNames.count;i++){
        UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:self.imageNames[i]]];
//        CGRect imageFrame=CGRectZero;
//        imageFrame.size=_Scroll_page.frame.size;
//        imageFrame.origin.y=0;
//        imageFrame.origin.x=i*_Scroll_page.frame.size.width;
//        image.frame=imageFrame;
        image.frame=CGRectMake(i*_Scroll_page.frame.size.width, 0, _Scroll_page.frame.size.width, _Scroll_page.frame.size.height);
        
        if (i== self.imageNames.count-1) {
            UIButton *bt_page=[[UIButton alloc]initWithFrame:CGRectMake(145, 500, 150, 50)];
            bt_page.backgroundColor=[UIColor greenColor];
            
            bt_page.backgroundColor=[UIColor greenColor];
            [bt_page setTitle:@"立 即 体 验" forState:UIControlStateNormal];
            bt_page.titleLabel.font=[UIFont systemFontOfSize:25.0];
            //imageView  关闭的时候所有手势都无效
            image.userInteractionEnabled=YES;
            [image addSubview:bt_page];
            [bt_page addTarget:self action:@selector(gotoView:) forControlEvents:UIControlEventTouchUpInside];
            
        }
       
        [_Scroll_page addSubview:image];

    }
    ;
    
    //设置滚动视图主页滚动
    _Scroll_page.pagingEnabled=YES;
    //设置边缘不可以弹跳
    _Scroll_page.bounces=NO;
    //设置水平滚动条隐藏
    _Scroll_page.showsHorizontalScrollIndicator=NO;
    _Scroll_page.showsVerticalScrollIndicator=NO;
    
    //把滚动视图加到view中
    [self.view addSubview:_Scroll_page];
    
    //配置分页控件
    _pageControl=[[UIPageControl alloc]init];
    _pageControl.frame=CGRectMake(0, _Scroll_page.frame.size.height-40, _Scroll_page.frame.size.width, 30);
    _pageControl.numberOfPages=self.imageNames.count;
    _pageControl.pageIndicatorTintColor=[UIColor redColor];
    _pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
    //关闭分页控件的用户交互功能
    _pageControl.userInteractionEnabled=NO;
    [self.view addSubview:_pageControl];
//    [self gotoView:nil];

}
-(void)gotoView:(id)sender{
    
    LoginViewController *loginViewController=[self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    [self presentViewController:loginViewController animated:YES completion:nil];
   
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x/SCREEN_WIDTH;
    NSLog(@"-----%d----%f",index,scrollView.contentOffset.x);
    _pageControl.currentPage = index;
}

@end
